package com.splashcard.net;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MechineServerAgent {
	private final static Logger logger = Logger.getLogger(SplashClient.class
			.getName());
	private String targetName;
	private int port;
	
	public MechineServerAgent(String targetName, int port) {
		super();
		this.targetName = targetName;
		this.port = port;
	}
	
	public Object getMonitorInfo(){
		return send(targetName, port, "getMonitorInfo");
	}
	
	public interface AgentCallback{
		public void OnCallBack(Object object);
	}

	private Object send(String targetName, int port, Object object) {
		Socket socket = null;
		ObjectOutputStream os = null;
		ObjectInputStream is = null;

		try {
			socket = new Socket(targetName, port);
			os = new ObjectOutputStream(socket.getOutputStream());
			os.writeObject(object);
			os.flush();

			is = new ObjectInputStream(new BufferedInputStream(
					socket.getInputStream()));
			Object obj = is.readObject();
			return obj;
		} catch (IOException ex) {
			logger.log(Level.SEVERE, null, ex);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (Exception ex) {
			}
			try {
				os.close();
			} catch (Exception ex) {
			}
			try {
				socket.close();
			} catch (Exception ex) {
			}
		}
		return null;
	}

}
